<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 给一个数组,要求数组内的两个数和等于一个给定的数值 时间复杂度O(n)
    const arr = [1, 2, 3, 4, 5] 
    const sum = 5
    // 1.for循环
    for (let i = 0; i < arr.length; i++) {
      for (let j = i + 1; j < arr.length; j++) {
        if (arr[i] + arr[j] === sum) {
          console.log(arr[i], arr[j]);
        }
      }
    }  //O(n^2)
    // 2.hash表
    // const hash = {}
    // arr.forEach((item,index)=>{
    //   console.log('次数')
    //   let a = sum - item
    //   if(a in hash){
    //     console.log(a,item);
    //   }
    //   hash[item]=index
    // })  // O(n)
    // console.log(hash)
  </script>
</body>

</html>